var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
Page({
  data: {
    resourceUrl: app.globalData.apiUrl + '/public/uploads/goods/',
    goods: '',
    img: [{ "img": "/../image/1.jpg"}],
    num: 1,
    totalNum: 0,
    hasCarts: false,
    curIndex: 0,
    show: false,
    scaleCart: false,
  },

  addCount() {
    let num = this.data.num;
    num++;
    this.setData({
      num: num
    })
  },
   reduceCount(){
     let num = this.data.num;
     if(num==1){
       num=1
     }
     else{
     num--;
     }
     this.setData({
       num: num
     })
   },
  addToCart(tap) {
    console.log(tap)
    console.log(this.data.totalNum + this.data.num)
    const self = this;
    const num = this.data.num;
    let total = this.data.totalNum;
    var goods_id = tap.target.dataset.id;//商品id
    var goods_name = tap.target.dataset.name;//商品名称
    var price = parseInt(tap.target.dataset.price);//商品价格
    var goods_pic = tap.target.dataset.pic;//图片
    var nums = this.data.totalNum + this.data.num;
    var integral = tap.target.dataset.integral;//获得的积分
    self.setData({
      show: true
    })
    setTimeout(function () {
      self.setData({
        show: false,
        scaleCart: true
      })
      setTimeout(function () {
        self.setData({
          scaleCart: false,
          hasCarts: true,
          totalNum: num + total
        })
      }, 200)
    }, 300)
    try{
      var arr = wx.getStorageSync('shopCarInfo');
      if(arr == ''){
          var arr = [];
        arr.push({ "id": goods_id, 'name': goods_name, 'price': price * nums, 'num': nums, 'pic': goods_pic, 'active': 1, 'integral': integral});
          wx.setStorageSync('shopCarInfo', arr); 
          
      }else{
          var arr = wx.getStorageSync('shopCarInfo');
          var a = [];
          for(i in arr){
             a.push(arr[i].id.toString());
          }
        var str = a.toString() 
        console.log(str.indexOf(goods_id))
        console.log(str)
        for (var i = 0, len = arr.length; i < len; i++){
            // console.log(arr[i].id)
            // console.log(goods_id)
          if (str.indexOf(goods_id)!=-1){
              arr[i].num = arr[i].num + num;
            arr[i].price = arr[i].price + num*price;
              
              wx.setStorageSync('shopCarInfo', arr);
              // console.log(1111111111)
              
          } else if (str.indexOf(goods_id) == -1){
              // console.log(222222222222)
              
           
              var arr = wx.getStorageSync('shopCarInfo');
              arr.push({ "id": goods_id, 'name': goods_name, 'price': price * nums, 'num': nums, 'pic': goods_pic,'active':1,'integral':integral });
              wx.setStorageSync('shopCarInfo', arr);
              break;
              
            }
            
            
          }
           
      }
    }catch (e){
        console.log(e)
    }
    var a = wx.getStorageSync('shopCarInfo');
    console.log(a)
  },

  bindTap(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index
    })
  },
  onLoad(options){
    console.log(options)
    var that = this;
    wx.request({
      url: app.globalData.apiUrl+'/api/goods/goodsInfo',
      data: {id:options.id},
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        console.log(res.data)
        //富文本
        WxParse.wxParse('content', 'html', res.data.content, that, 0);
        that.setData({goods:res.data})
      },
      fail: function(res) {},
      complete: function(res) {},
    })

  }
})